<?php

$config = require_once 'config/base.php';
require_once 'context.php';

define('PRODUCTION', $_SERVER['HTTP_HOST'] != $config['system']['internal_host']);
define('FS_KEY', 'ifaj@*()($*o-kfas944-=53');
define('FS_DOMAIN' , 'http://www.stage.fotostrana.ru');
if (!PRODUCTION) {
    ini_set('display_errors', 'on');
    ini_set('max_upload_filesize', '16M');
    set_time_limit(0);
    error_reporting(0);
}

ini_set('memory_limit', '1024M');
Context::set('config', $config);

if (PRODUCTION && !isset($_SERVER['PHP_AUTH_USER']) &&
   ($_SERVER['PHP_AUTH_USER'] != $config['system']['admin_name'] || $_SERVER['PHP_AUTH_PW'] != $config['system']['admin_password'])) {
    header('WWW-Authenticate: Basic realm="Hello"');
    header('HTTP/1.0 401 Unauthorized');
}

$connection = new PDO("mysql:host={$config['db']['host']};dbname={$config['db']['dbname']}", $config['db']['username'], $config['db']['password']);

if (!PRODUCTION) {
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

Context::set('db', $connection);

require_once 'tools.php';
$user = new User(json_decode($_COOKIE['user']));